/**
 * Created by zengy on 2017/7/18.
 */
angular.module('mainApp.service', ['ui.router'])
    .config(function($stateProvider) {
        $stateProvider
            .state({//模板列表页面
                name : 'service/serviceList',
                url:'/service/serviceList',
                templateUrl:'views/service/service_list.html',
                controller:'serviceCtrl'
            })
            .state({
                name:'service/serviceDetail',
                url:'/service/serviceDetail/:wxServicesId',
                templateUrl:'views/service/service_detail.html',
                controller:'serviceDetail'
            })
    })
.controller('serviceCtrl',function ($scope) {
    $scope.basePictureUrl=basePictureUrl
    $scope.$emit('arg',9)
    layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
    new HttpRequest().getWxServicesList(function (res) {
        layer.closeAll();
        if(res.code=='000'){
            $scope.serviceList=res.data;
            $scope.$apply();
        }
    })
    //删除
    $scope.delServer=function (event) {
        var _this = $(event.target);
        var wxServicesId = _this.attr('data-id')
        if (wxServicesId) {
            var index = layer.confirm('是否要删除本条套餐服务？', {
                title: '删除确认',
                btn: ['确定', '取消'],//按钮
            }, function () {
                new HttpRequest().deleteWxServices({
                    wxServicesId: wxServicesId
                }, function (res) {
                    if (res.code == '000') {
                        location.reload()
                    } else {
                        layer.msg('删除失败！');
                    }
                })
            }, function () {
                layer.close(index)
            })

        }
    }

})
.controller('serviceDetail',function ($scope,$stateParams) {
    $scope.wxServicesId=$stateParams.wxServicesId;
    $scope.basePictureUrl=basePictureUrl;
    $scope.serviceModel={};
    new HttpRequest().getOneByServicesId({wxServicesId:$scope.wxServicesId},function (res) {
        if(res.code=='000'){
            $scope.serviceModel=res.data;
            $('select[name="serviceType"]').find('option[value="'+res.data.serviceType+'"]').prop('selected',true)
            $scope.$apply();
        }
    })
    //上传封面功能
    $scope.uploadImg = function (current, type) {
        if(current.files.length>0){
            $('.imgCover').attr('src','./img/img-cover.png');
            new HttpRequest().uploadFile(current,type,function (res) {
                if(res.code == '000'){
                    var resData = JSON.parse(res.data).url;
                    $scope.serviceModel.icon = resData;
                    $('.imgCover').attr('src',basePictureUrl+resData);
                }else{
                    layer.msg('上传封面失败'+'('+res.msg+')');
                }
            })
        }
    };
    $scope.commit=function () {
        var param={
            rowId:$scope.wxServicesId,
            icon:$scope.serviceModel.icon,
            name:$scope.serviceModel.name,
            typeDesc:$scope.serviceModel.typeDesc,
            serviceType:$('select[name="serviceType"]').val(),
            parameter:$scope.serviceModel.parameter,
            content:$scope.serviceModel.content
        }
        new HttpRequest().modifyWxServices(param,function (res) {
            if(res.code=='000'){
                location.reload()
            }else{
                layer.msg(res.msg);
            }
        })
    }

})
